package com.server.impl;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.entity.TTreeNodeReq;
import com.entity.TTreeNode;
import com.mapper.db1.TTreeNodeMapper;
import com.server.ITTreeNodeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chl
 * @since 2021-12-18
 */
@Service
public class TTreeNodeServiceImpl extends ServiceImpl<TTreeNodeMapper, TTreeNode> implements ITTreeNodeService {


    @Transactional(value = "test1TransactionManager",propagation = Propagation.REQUIRES_NEW)
    @Override
    public Boolean add(TTreeNodeReq tTreeNode) {


        TTreeNode node = new TTreeNode();
        node.setAddress("我的地址矮矮的");
        node.setAge(20);
        node.setName("事务测试");
        boolean save = save(node);

        Long treeId = node.getTreeId();
        System.out.println("生成ID为:" + treeId);
//        int z = 10 / 0;
        return save;

    }

    @Override
    public PageResult<List<TTreeNode>> listPage(TTreeNodeReq tTreeNodeReq) {


        Page<TTreeNode> page = page(new Page<>(tTreeNodeReq.getPageNumber(), tTreeNodeReq.getPageSize()),
                new LambdaQueryWrapper<TTreeNode>()
                        .in(CollectionUtils.isNotEmpty( tTreeNodeReq.getIdList()),TTreeNode::getTreeId,
                                tTreeNodeReq.getIdList()));
        List<TTreeNode> records = page.getRecords();
        PageResult pageResult = new PageResult(tTreeNodeReq.getPageNumber(), tTreeNodeReq.getPageSize());
        pageResult.setTotal((int) page.getTotal());
        pageResult.addAll(records);
        return pageResult;


    }
}
